package com.yoda.mall.admin.controller;

import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.admin.service.OrderReturnApplyService;
import com.yoda.mall.api.OrderReturnApplyApi;
import com.yoda.mall.model.dto.ReturnApplyPageDto;
import com.yoda.mall.model.dto.StatusDto;
import com.yoda.mall.model.entity.oms.OrderReturnApply;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import com.yoda.mall.model.vo.ReturnApplyVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @author shawn
 * @date 2022年 12月 18日 20:59
 */
@RestController
@RequestMapping("/returnApply")
public class OrderReturnApplyController {
    ///returnApply/list?pageNum=1&pageSize=10

    @Resource
    private OrderReturnApplyService orderReturnApplyService;


    @GetMapping("/list")
    public R getReturnApplyByPage(ReturnApplyPageDto dto) throws ParseException {
        PageResult<OrderReturnApply> pageResult = orderReturnApplyService.getReturnApplyByPage(dto);
        return R.success(pageResult);
    }

    /**
     * 根据申请id批量删除已拒绝申请
     *
     * @param ids id
     * @return {@link R}
     */
    @PostMapping("/delete")
    public R deleteReturnApply(@RequestParam List<Long> ids){
        Integer count = orderReturnApplyService.deleteReturnApply(ids);
        if (count==0){
            throw new BusinessException(ErrorResult.builder().errMessage("仅能删除已拒绝订单").errCode(10000).build());
        }
        return R.success(count);
    }


    @GetMapping("/{id}")
    public R getReturnApplyById(@PathVariable Long id){
        ReturnApplyVo vo = orderReturnApplyService.getReturnApplyById(id);
        return R.success(vo);
    }

    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable Long id, @RequestBody StatusDto statusParam){
        statusParam.setId(id);
        Integer count = orderReturnApplyService.updateStatus(statusParam);
        return R.success(count);
    }
}
